<?php
	session_start();
	if(!$_SESSION["username"]){
		//not logged in
		header("location:login.php");
	}

	// Connect to server and select databse.
	mysql_connect("localhost", "root", "")or die("cannot connect"); 
	mysql_select_db("Project 5.5")or die("cannot select DB");
	
	//show student-related statistics
	function studentStats() {
	
		$sql='SELECT u.User_ID, u.First_Name, u.Last_Name, s.Class_ID, s.Group_ID FROM users u,studentinfo s WHERE u.Role="STU" AND u.User_ID=s.User_ID';
		$result=mysql_query($sql);
		
		$i=0;
		while ($row = mysql_fetch_row($result)) {
		
			$sql2='SELECT r.Assessment_ID, a.Due_Date, r.Date_Recieved, a.Weighting, r.Mark FROM results r, assessment a WHERE r.Student_ID="'.$row[0].'" GROUP BY r.Assessment_ID';
			$result2=mysql_query($sql2);
			$mark=0;
			
			while ($row2 = mysql_fetch_row($result2)) {
				$mark+=$row2[4];
			}
			$marks[$i]=$mark;
			$i++;
		}
		
		//calculate marks of due assignments
		
		$markssofar=0;
		$marksremaining=100;
		
		$sql="SELECT Weighting, Due_Date FROM assessment";
		$result=mysql_query($sql);
		while ($row = mysql_fetch_row($result)) {
			$today = getdate();
			$due = explode("/", $row[1]);
			
			if (($today['year']>$due[2]) || ($today['year']>=$due[2] && $today['mon']>$due[1]) || ($today['year']>=$due[2] && $today['mon']>=$due[1] && $today['mday']>$due[0])) {
				$markssofar+=$row[0];
				$marksremaining-=$row[0];
				
			}
		}
		
		//calculate min/max/average/7s/fails
		$max=0;
		$min=100;
		$sum=0;
		$num7s=0;
		$numfails=0;
		foreach ($marks as $m) {
			$sum += $m;
			if ($max < $m) {
				$max=$m;
			}else if ($min > $m) {
				$min=$m;
			}
			if ($m >= (85 - $marksremaining)) {
				$num7s++;
			}
			if ($m < (50 - $marksremaining)) {
				$numfails++;
			}
		}
		$average=$sum/$i;
		$average = number_format($average, 2, '.', '');
		
		//display stats
		echo "Number of students: ".($i)."<br/>";
		echo "Average Marks: ".$average."/".$markssofar."<br/>";
		echo "Highest Marks: ".$max."<br/>";
		echo "Lowest Marks: ".$min."<br/>";
		echo "Students that can still get a 7: ".$num7s."<br/>";
		echo "Students that cannot pass: ".$numfails."<br/>";
	}
	
	//calculate assessment related statisitics
	function assessmentStats() {
		
		$sql='SELECT User_ID FROM studentinfo';
		$result=mysql_query($sql);
		$numStudents=mysql_num_rows($result);
		
		$sql='SELECT Assessment_ID, Assessment_Name, Weighting, Due_Date FROM assessment';
		$result=mysql_query($sql);
				
		$i=1;
		while ($row = mysql_fetch_row($result)) {
			$sql2='SELECT Mark FROM results WHERE Assessment_ID="'.$row[0].'"';
			$result2=mysql_query($sql2);
			$numSubmissions=mysql_num_rows($result2);
			
			$j=0;
			while ($row2 = mysql_fetch_row($result2)) {
				$marks[$j]=$row2[0];
				$j++;
			}

			$today = getdate();
			$due = explode("/", $row[3]);			
			
			echo "<u>Assessment $i - ".$row[1]."</u><br/>";
			echo "Due: ".$row[3]."<br/>";
			echo "Weighting: ".$row[2]."<br/>";
			
			//if past due calc stats
			if (($today['year']>$due[2]) || ($today['year']==$due[2] && $today['mon']>$due[1]) || ($today['year']==$due[2] && $today['mon']==$due[1] && $today['mday']>$due[0])) {
				$max=0;
				$min=$row[2];
				$sum=0;
				foreach ($marks as $m) {
					$sum += $m;
					if ($max < $m) {
						$max=$m;
					}else if ($min > $m) {
						$min=$m;
					}
				}
				$average=$sum/$j;
				$average = number_format($average, 2, '.', '');
				
				echo "Number of Submissions: ".$numSubmissions."/".$numStudents."<br/>";
				echo "Average Mark: ".$average."<br/>";
				echo "Highest Mark: ".$max."<br/>";
				echo "Lowest Mark: ".$min."<br/>";
			}
			
			echo "<br/>";
			$i++;
		}
	}
	
	//display results graph
	function overallGraph() {
	
		$markssofar=0;
		$marksremaining=100;
		$sql="SELECT Weighting, Due_Date FROM assessment";
		$result=mysql_query($sql);
		//calculate marks of due assignments
		while ($row = mysql_fetch_row($result)) {
			$today = getdate();
			$due = explode("/", $row[1]);
			
			if (($today['year']>$due[2]) || ($today['year']>=$due[2] && $today['mon']>$due[1]) || ($today['year']>=$due[2] && $today['mon']>=$due[1] && $today['mday']>$due[0])) {
				$markssofar+=$row[0];
				$marksremaining-=$row[0];
				
			}
		}
		
		$sql='SELECT u.User_ID, u.First_Name, u.Last_Name, s.Class_ID, s.Group_ID FROM users u,studentinfo s WHERE u.Role="STU" AND u.User_ID=s.User_ID';
		$result=mysql_query($sql);
		
		echo "Hover over a bar to see the students name and current mark.<br/>The bar on the left shows the current maximum marks available in green.";
		
		echo "<div id='graph'>";
		echo "<div style='display:inline-block;vertical-align:top;margin-right:3px;padding-top:".($marksremaining-13)."px;'>".$markssofar."</div>";
		echo "<div id='graphscale'>";
		
		echo "<span id='graphremaining' style='height:".$marksremaining."px'></span><br/>";
		echo "<span id='graphsofar' style='height:".$markssofar."px'></span>";
		echo "</div>";
		$i=0;
		
		while ($row = mysql_fetch_row($result)) {
			
			$sql2='SELECT r.Assessment_ID, a.Due_Date, r.Date_Recieved, a.Weighting, r.Mark FROM results r, assessment a WHERE r.Student_ID="'.$row[0].'" GROUP BY r.Assessment_ID';
			$result2=mysql_query($sql2);
			
			$mark=0;
			
			while ($row2 = mysql_fetch_row($result2)) {
				$mark+=$row2[4];
			}
			//display student bar and data
			echo "<div class='graphcol' style='height:".$mark."px;'></div>";
			echo "<span class='graphdata'>Student: ".$row[1]." ".$row[2]."<br/>Mark: ".$mark."</span>";
			$i++;
		}
		echo "</div>";
		
		$markssofar=0;
		$marksremaining=100;
		
		
		
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Statistics</title>	
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="js/formsValidate.js"/></script>
</head>
<body>

	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href='statistics.php'>Statistics</a>
		</div>
		<div id="logout"> 
			<a href="login.php?destroy=true">Log Out</a>
		</div>
	</div>
	
	<div id="error">
	</div>
	<div id="namebar_stats" ></div>
	
	<div id="body">
		<br/>
		<table id="form4" align="center" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td width="59%" style="padding:15px;vertical-align:top;">
					<div style="font-size:14pt; font-weight:bold; text-decoration:none; padding-bottom:15px; background-color:#FFF; text-align:center">
						<div style="background-color: #CCC;font-size:25pt;font-weight:bold;">
							Student Statistics
						</div>
						<p>
							<?php
								studentStats();
							?>
						</p>
					</div>
					<div style="font-size:14pt; font-weight:bold; text-decoration:none; margin-top:15px; padding-bottom:15px; background-color:#FFF; text-align:center">
						<div style="background-color: #CCC;font-size:17pt;text-align:center;">
							Overall Results
						</div>
						<p>
							<?php
								overallGraph();
							?>
						</p>
					</div>
				</td>
				<td width="59%" style="padding:15px;vertical-align:top;">
					<div style="font-size:14pt; font-weight:bold; text-decoration:none; padding-bottom:15px; background-color:#FFF; text-align:center">
						<div style="background-color: #CCC;font-size:25pt;font-weight:bold;">
							Assessment Statistics
						</div>
						<p>
							<?php
								assessmentStats();
							?>
						</p>
					</div>
					<br/>
				</td>
			</tr>
		</table>
	</div>
	
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>